package com.mhwx.mhlive.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cnlive.libs.util.Config;
import com.cnlive.libs.video.video.VideoView;
import com.cnlive.libs.video.video.base.IDataSource;
import com.cnlive.libs.video.video.base.IMediaPlayer;
import com.mhwx.mhlive.R;
import com.mhwx.mhlive.ui.adapter.GridViewAdapter;
import com.mhwx.mhlive.ui.base.BaseActivity;
import com.mhwx.mhlive.ui.widget.DialogHear;
import com.mhwx.mhlive.util.DimenUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import tyrantgit.widget.HeartLayout;

/**
 * 类功能描述
 *  直播详情
 * @author Guol
 * @version 1.0
 * @copyright 北京明华维新信息技术有限公司
 * @time 2017/5/3
 */
public class LiveDetails  extends BaseActivity {

    @BindView(R.id.videoview)
     VideoView videoView;
    @BindView(R.id.header)
    HeartLayout heartLayout;
    @BindView(R.id.ll_gallery)
    LinearLayout mGallery ;

  @BindView(R.id.gridview)
  GridView gridView;

    @BindView(R.id.img_hear)
    ImageView img_hear;

    private Random mRandom = new Random();
    private Timer mTimer = new Timer();
    private DialogHear dialogHear;
     private List<String> list;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.live_details);


        initPlay();
        initView();
        setData();
        setGridView();
    }

    private void initView(){
        mTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                heartLayout.post(new Runnable() {
                    @Override
                    public void run() {
                        heartLayout.addHeart(randomColor());
                    }
                });
            }
        }, 500, 500);

         dialogHear=new DialogHear(this);
        img_hear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogHear.show();
            }
        });
    }

    private void setGridView(){
        int size = list.size();
        int length = (int) DimenUtil.dp2px(this,25);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        int gridviewWidth = (int) (size * (length + 4) * density);
        int itemWidth = (int) (length * density);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                gridviewWidth, LinearLayout.LayoutParams.FILL_PARENT);
        gridView.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
        gridView.setColumnWidth(itemWidth); // 设置列表项宽
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setNumColumns(size); // 设置列数量=列表集合数

        GridViewAdapter adapter = new GridViewAdapter(this,list);
        gridView.setAdapter(adapter);
    }
    private void setData(){
        list=new ArrayList<>();
        for (int i=0;i<20;i++){
            list.add(i+"");
        }
    }


    private int randomColor() {
        return Color.rgb(mRandom.nextInt(255), mRandom.nextInt(255), mRandom.nextInt(255));
    }

    @Override
    protected void onPause() {
        super.onPause();
//        videoView.pause();
         MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        videoView.start();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        videoView.release();
        mTimer.cancel();
    }

    private void initPlay() {
        //认证APP播放权限（使用视讯中国资源）
        videoView.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(IMediaPlayer mediaplayer) {
                mediaplayer.start();
            }
        });

        startPlay();
    }

    private void startPlay() {
//        //视讯云 直播
//        videoView.setDataSource(new ProgramLive(),false);

        //视讯云 点播
        videoView.setDataSource(new IDataSource() {
            @Override
            public String getDataSourceId() {
                return "117_0005701481101785734a";
            }

            @Override
            public String getDataSourceType() {
                return Config.TYPE_VOD;
            }

            @Override
            public String getDataSourceRate() {
                return Config.TYPE_PLAY_RATE_2;
            }
        });

//        //本地视频
//        try {
//            videoView.setDataSource("rtmp://live.hkstv.hk.lxdns.com/live/hks");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

}
